typedef unsigned int uint;

uint pixelBits = 10;
uint patchBits, pixelMask, patchMask;

//uint3 deg, seg, segShift;
//float3 boxSize, box0;
//Replaced by asi
ABSSURF_SHARE_INFO asi;

uint nPatch;
int degSize;

#ifndef UNIFORM_KNOT
__constant__ float* dBoxX = 0;
__constant__ float* dBoxY = 0;
__constant__ float* dBoxZ = 0;
#endif

__device__ int* dPatchIndex = 0;
__device__ int* dPatchIndexInverse = 0;
__device__ float* dFuncData = 0;

__device__   int* dPolarIndex = 0;
__device__	 float* dPolarData = 0;


//the variable below are defined in the way described in "Progress 06.22"
int maxSeg;
//uint maxSilPoint;
//uint actualSilPoint;

uint allPointSize;
//int maxPoint;
//int actualPoint;

//__device__ size_t* dMaxSilPoint  = 0;
__device__ size_t* dNumber = 0;

__device__ uint* dSilOccupied = 0;
__device__ uint* dSilOccupiedValid = 0;
__device__ uint* dPointOccupied = 0;
__device__ uint* dPointOccupiedValid = 0;
__device__ uint* dOccupiedCompact = 0;
__device__ uint* dOccupiedSort = 0;
__device__ uint* dOccupiedSortPad = 0;

__device__ float* dSil = 0;
//__device__ uint* dError = 0;
//__device__ uint* dBoundary = 0;

__device__ int* dDebugInfo = 0;

#if TEST
__device__ int*		dSilContour = 0;
__device__ float*	dSilContourInfo = 0;
__device__ int*		dConverEdge = 0;
__device__ float*	dConverEdgeInfo = 0;
__device__ int*		dSilConverEdge = 0;
int* hSilContour = 0;
int* hSilContourInfo = 0;
#endif


uint* pointOccupiedSort = 0;
uint* pointOccupiedPad = 0;
//uint* silPointOccupied = 0;
//uint* silPointOccupiedPad = 0;

CUDPPHandle plan;

__constant__ RENDER_PARAM cRenderParam;
int renderWidth, renderHeight;
//float4 viewProj0, viewProj1, viewProj2, viewProj3;
float4 invViewProj0, invViewProj1, invViewProj2, invViewProj3;
//float3 eye;
//float errorXTol;
//LIGHT light[3];
//MATERIAL material;
//int nLight;


texture<float, 1, cudaReadModeElementType> polarTex;






void compact(uint* compactNum, uint allNum, size_t* dNum, uint* Occupied, uint* OccupiedValid, uint* Compact, uint saveSize, uint* SaveData)
{
	CUDPPConfiguration config;
	CUDPPResult result = CUDPP_SUCCESS;	

	CUDA_SAFE_CALL( cudaMemset(Compact, 0, allNum) );
	
	config.algorithm = CUDPP_COMPACT;
    config.datatype = CUDPP_UINT;
	config.options = CUDPP_OPTION_FORWARD;
	result = cudppPlan(&plan, config, allNum/sizeof(uint), 1, 0);
	if (result != CUDPP_SUCCESS)
	{
		printf("Error creating plan for compact\n");
		return;
	}
	result = cudppCompact(plan, Compact, dNum, Occupied, OccupiedValid, allNum/sizeof(uint));
	if (result != CUDPP_SUCCESS)
	{
		printf("Error compacting !\n");
		return;
	}	
	result = cudppDestroyPlan(plan);    
    if (result != CUDPP_SUCCESS)
    {
        printf("Error destroying CUDPPPlan!\n");
		return;
    }

	CUDA_SAFE_CALL( cudaMemcpy(compactNum, dNum, sizeof(size_t), cudaMemcpyDeviceToHost) );

#if DEBUG_BUFFERS
	printf("dOccupiedCompact:\n");
	dumpBuffer(Compact, *compactNum);
#endif

	if(saveSize!=0)
	{
		if(saveSize<(*compactNum))
			printf("Malloc more memory for SaveData!\n");

		CUDA_SAFE_CALL( cudaMemset(SaveData, 0, saveSize*sizeof(uint)) );
		CUDA_SAFE_CALL( cudaMemcpy(SaveData, Compact, (*compactNum)*sizeof(uint), cudaMemcpyDeviceToDevice) );
		CUDA_SAFE_CALL( cudaMemset(Compact, 0, allNum) );
	}
}

void sortPadAll(uint sortNum, uint* sortPad, uint allNum, uint* Unsort, uint* Sort, uint* SortPadData)	
{
	CUDPPConfiguration config;
	CUDPPResult result = CUDPP_SUCCESS;	

	CUDA_SAFE_CALL( cudaMemset(Sort, 0, sortNum*sizeof(uint)) );

	config.algorithm = CUDPP_SORT_RADIX;
	config.datatype = CUDPP_UINT;
	config.op = CUDPP_ADD;
	config.options = CUDPP_OPTION_KEYS_ONLY;	
	result = cudppPlan(&plan, config, sortNum, 1, 0);
	if (result != CUDPP_SUCCESS)
	{
		printf("Error creating plan for SortBit\n");
		return;
	}
	result = cudppSort(plan, Unsort, 0, sortNum, patchBits);
	Sort = Unsort;
	if (result != CUDPP_SUCCESS)
	{
		printf("Error SortBit\n");
		return;
	}
	result = cudppDestroyPlan(plan);    
    if (result != CUDPP_SUCCESS)
    {
        printf("Error destroying CUDPPPlan!\n");
    }


#if DEBUG_BUFFERS
	printf("dOccupiedSort:\n");
	dumpBuffer(Sort, sortNum);
#endif

	CUDA_SAFE_CALL( cudaMemcpy(pointOccupiedSort, Sort, sortNum*sizeof(uint), cudaMemcpyDeviceToHost) );
	pointOccupiedSort[sortNum] = 0;
	uint *p1=pointOccupiedSort, *p2=pointOccupiedPad;
	uint sortPadNum=0, local=0, np = (*p1)&patchMask;
	while(*p2++ = *p1++)
	{
		local++;
		if( (*p1&patchMask)!=np || local==PIXELTHREADS)
		{
			for(int i=local; i<PIXELTHREADS; i++)
				*p2++ = 0;
			sortPadNum++;
			local = 0;
			np = (*p1&patchMask);
		}		
	}
	if(local)
	{
		for(int i=local; i<PIXELTHREADS; i++)
			*p2++ = 0;
	}
	sortPadNum *= PIXELTHREADS;
	//printf("%d %d\n", silSize, sortPadNum);

	if(sortPadNum*sizeof(uint)>allNum*5/3)
		printf("Allocate more GPU memeory for dSilOccupiedSortPad!\n");

	(*sortPad) = sortPadNum;
 
	CUDA_SAFE_CALL( cudaMemset(SortPadData, 0, sortPadNum*sizeof(uint)) );
	CUDA_SAFE_CALL( cudaMemcpy(SortPadData, pointOccupiedPad, sortPadNum*sizeof(uint), cudaMemcpyHostToDevice) );

#if DEBUG_BUFFERS
	printf("dPointOccupiedSortPad:\n");
	printf("sortPadNum=%d\n", sortPadNum);
	dumpBuffer(SortPadData, sortPadNum);
#endif
}



__constant__ float* dInvMatrix = 0;
void InitInvMatrix(int degSum)
{
	//float invData[]={1.0000000000000000f,-0.0000000000000000f, 0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-0.5000000000000000f,2.0000000000000000f,-0.5000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-0.8333333333333337f,3.0000000000000000f,-1.5000000000000000f,0.3333333333333334f,0.3333333333333336f,-1.5000000000000000f,3.0000000000000000f,-0.8333333333333333f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.0833333333333333f,4.0000000000000000f,-3.0000000000000000f,1.3333333333333333f,-0.2499999999999999f,0.7222222222222220f,-3.5555555555555549f,6.6666666666666661f,-3.5555555555555554f,0.7222222222222221f,-0.2500000000000000f,1.3333333333333333f,-3.0000000000000000f,4.0000000000000000f,-1.0833333333333333f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.2833333333333341f,5.0000000000000018f,-5.0000000000000036f,3.3333333333333357f,-1.2500000000000004f,0.2000000000000001f,1.1208333333333347f,-6.0416666666666705f,12.2916666666666750f,-9.5833333333333375f,3.8541666666666670f,-0.6416666666666668f,-0.6416666666666668f,3.8541666666666670f,-9.5833333333333357f,12.2916666666666679f,-6.0416666666666661f,1.1208333333333336f,0.2000000000000003f,-1.2500000000000007f,3.3333333333333344f,-5.0000000000000000f,4.9999999999999991f,-1.2833333333333337f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.4499999999999997f,5.9999999999999982f,-7.4999999999999893f,6.6666666666666519f,-3.7499999999999885f,1.1999999999999948f,-0.1666666666666652f,1.5133333333333334f,-8.8800000000000008f,20.0999999999999801f,-20.5333333333332995f,12.2999999999999687f,-4.0799999999999823f,0.5799999999999965f,-1.1349999999999982f,7.5599999999999952f,-20.9249999999999687f,29.9999999999999538f,-20.9249999999999581f,7.5599999999999739f,-1.1349999999999953f,0.5799999999999977f,-4.0799999999999956f,12.2999999999999776f,-20.5333333333332959f,20.0999999999999623f,-8.8799999999999741f,1.5133333333333288f,-0.1666666666666658f,1.1999999999999984f,-3.7499999999999920f,6.6666666666666536f,-7.4999999999999849f,5.9999999999999902f,-1.4499999999999984f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.5928571428571405f,6.9999999999999813f,-10.4999999999999680f,11.6666666666666359f,-8.7499999999999822f,4.1999999999999940f,-1.1666666666666661f,0.1428571428571439f,1.8939153439153391f,-12.0166666666666195f,30.2749999999999098f,-38.1759259259258315f,30.3333333333332682f,-15.0499999999999705f,4.2712962962962884f,-0.5309523809523826f,-1.7016269841269820f,12.4211111111110757f,-38.1091666666666171f,62.2611111111110986f,-55.9513888888889142f,29.7966666666667024f,-8.8530555555555708f,1.1363492063492142f,1.1363492063492049f,-8.8530555555555264f,29.7966666666666384f,-55.9513888888889213f,62.2611111111111768f,-38.1091666666667237f,12.4211111111111308f,-1.7016269841269906f,-0.5309523809523805f,4.2712962962962822f,-15.0499999999999901f,30.3333333333333570f,-38.1759259259259736f,30.2750000000000377f,-12.0166666666666764f,1.8939153439153462f,0.1428571428571432f,-1.1666666666666639f,4.1999999999999984f,-8.7500000000000107f,11.6666666666666856f,-10.5000000000000142f,7.0000000000000044f,-1.5928571428571432f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.7178571428571361f,7.9999999999999449f,-13.9999999999998117f,18.6666666666662842f,-17.4999999999995239f,11.1999999999996280f,-4.6666666666664867f,1.1428571428570935f,-0.1249999999999938f,2.2606575963718631f,-15.4122448979590096f,42.9714285714279427f,-64.4063492063479259f,63.7142857142841450f,-42.0571428571416348f,17.8793650793644900f,-4.4408163265304488f,0.4908163265305925f,-2.3215986394557424f,18.3981859410427475f,-62.1396825396813597f,115.1238095238071395f,-126.3888888888859725f,88.6603174603151984f,-39.2190476190465489f,10.0172335600904177f,-1.1303287981859089f,1.8572789115645787f,-15.8360090702943861f,58.8799999999985815f,-123.8552380952352792f,158.9079365079331296f,-123.8552380952355634f,58.8799999999988444f,-15.8360090702944873f,1.8572789115645936f,-1.1303287981858976f,10.0172335600903608f,-39.2190476190464565f,88.6603174603152127f,-126.3888888888862141f,115.1238095238075374f,-62.1396825396816652f,18.3981859410428754f,-2.3215986394557628f,0.4908163265305899f,-4.4408163265304355f,17.8793650793644687f,-42.0571428571416703f,63.7142857142843297f,-64.4063492063481959f,42.9714285714281345f,-15.4122448979590789f,2.2606575963718738f,-0.1249999999999929f,1.1428571428570891f,-4.6666666666664884f,11.1999999999996618f,-17.4999999999995985f,18.6666666666663730f,-13.9999999999998721f,7.9999999999999698f,-1.7178571428571410f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.8289682539682375f,8.9999999999998614f,-17.9999999999995097f,27.9999999999989875f,-31.4999999999986535f,25.1999999999987665f,-13.9999999999991971f,5.1428571428567720f,-1.1249999999998916f,0.1111111111110987f,2.6132688492062899f,-19.0366071428566386f,58.3232142857125240f,-101.2249999999963279f,119.7843749999950944f,-98.6624999999954468f,55.8624999999970058f,-20.7964285714271924f,4.5944196428567423f,-0.4572420634920196f,-2.9806866496597464f,25.4493941326520705f,-93.9507015306088249f,195.4687499999928377f,-253.3419642857044778f,220.1825892857050064f,-129.1459821428509542f,49.2922193877522048f,-11.0935905612236496f,1.1199723639454859f,2.7285306831064315f,-25.2269770408150364f,102.6958545918321875f,-240.1303571428474584f,352.2314732142723983f,-333.2491071428443092f,207.3290178571340903f,-82.5864795918325569f,19.1951690051008228f,-1.9871244331064410f,-1.9871244331064408f,19.1951690051008477f,-82.5864795918325996f,207.3290178571340618f,-333.2491071428448208f,352.2314732142736489f,-240.1303571428487089f,102.6958545918326422f,-25.2269770408151288f,2.7285306831064382f,1.1199723639454904f,-11.0935905612237136f,49.2922193877524535f,-129.1459821428514658f,220.1825892857060580f,-253.3419642857059557f,195.4687499999940599f,-93.9507015306092796f,25.4493941326521735f,-2.9806866496597610f,-0.4572420634920233f,4.5944196428567849f,-20.7964285714273558f,55.8624999999974037f,-98.6624999999962711f,119.7843749999961886f,-101.2249999999972090f,58.3232142857128650f,-19.0366071428567238f,2.6132688492062996f,0.1111111111111004f,-1.1249999999999065f,5.1428571428568279f,-13.9999999999993214f,25.1999999999990045f,-31.4999999999989662f,27.9999999999992326f,-17.9999999999996092f,8.9999999999998845f,-1.8289682539682401f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-1.9289682539682653f,10.0000000000000906f,-22.5000000000003304f,40.0000000000007745f,-52.5000000000010800f,50.4000000000011070f,-35.0000000000007887f,17.1428571428575012f,-5.6250000000000941f,1.1111111111111223f,-0.0999999999999999f,2.9521604938272010f,-22.8659611992948619f,76.4484126984138896f,-150.7231040564401781f,207.5462962963002553f,-204.8444444444484418f,144.8456790123483700f,-71.8518518518529845f,23.7996031746033907f,-4.7354497354497376f,0.4286596119929387f,-3.6685534244562890f,33.5333994709001360f,-134.4159226190498941f,310.9413580246965125f,-464.4618055555625915f,481.1416666666734727f,-351.2596450617326127f,178.2341269841284372f,-60.0409226190476915f,12.1033583186360048f,-1.1070601851851620f,3.7357268833460391f,-37.1741412614436584f,164.4347600151197355f,-423.9380196523106861f,702.4559082892477591f,-781.6052910052954985f,599.8974867724881506f,-315.7193247669428615f,109.3405139833699593f,-22.5239355001255177f,2.0963162425463295f,-3.1131057361216783f,32.5045141513400253f,-152.3329239103061639f,420.9183673469409541f,-756.3106261022934405f,917.6675485008795476f,-756.3106261022875287f,420.9183673469333939f,-152.3329239103017869f,32.5045141513386113f,-3.1131057361214829f,2.0963162425464601f,-22.5239355001262744f,109.3405139833714372f,-315.7193247669430320f,599.8974867724823525f,-781.6052910052826519f,702.4559082892316155f,-423.9380196522973279f,164.4347600151130848f,-37.1741412614416902f,3.7357268833457837f,-1.1070601851851993f,12.1033583186360953f,-60.0409226190470946f,178.2341269841246287f,-351.2596450617223240f,481.1416666666571587f,-464.4618055555454248f,310.9413580246838933f,-134.4159226190439824f,33.5333994708984733f,-3.6685534244560767f,0.4286596119929451f,-4.7354497354496727f,23.7996031746026624f,-71.8518518518500713f,144.8456790123417193f,-204.8444444444386363f,207.5462962962902793f,-150.7231040564330726f,76.4484126984106496f,-22.8659611992939844f,2.9521604938270940f,-0.0999999999999991f,1.1111111111110854f,-5.6249999999998250f,17.1428571428565704f,-34.9999999999987850f,50.3999999999982506f,-52.4999999999982165f,39.9999999999987352f,-22.4999999999994067f,9.9999999999998437f,-1.9289682539682356f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-2.0198773448773766f,11.0000000000001972f,-27.5000000000008313f,55.0000000000021032f,-82.5000000000037801f,92.4000000000048800f,-77.0000000000045475f,47.1428571428604215f,-20.6250000000018296f,6.1111111111118390f,-1.1000000000001844f,0.0909090909091212f,3.2780349927851264f,-26.8810317460325905f,97.4525793650830536f,-215.0718253968351519f,337.7327380952563658f,-388.4246666666913370f,329.3338888889129521f,-204.1023809523989314f,90.1050595238196337f,-26.8845238095277814f,4.8661031746041559f,-0.4039754689756156f,-4.3775525960668054f,42.6106388888909393f,-184.3563260582100725f,469.8954343033752252f,-794.5948214286172515f,955.2283888889515993f,-834.0169104938889859f,527.7551190476650618f,-236.6382638889143664f,71.4608193709680535f,-13.0592043650817260f,1.0926783309286776f,4.8653371997092938f,-51.7887376175816740f,247.3030965241205763f,-697.6824145723586525f,1291.0440217152627156f,-1651.5351959877896206f,1506.3864783952003563f,-984.2496748237388147f,452.2089481371840520f,-139.1943156599282929f,25.8312329879535412f,-2.1887762980339573f,-4.5128199923705488f,50.5845892778454882f,-256.3575489667536544f,773.6018335459823447f,-1539.9760505245064905f,2116.6332104278790212f,-2046.8703877316793296f,1400.4260546581267590f,-667.3178725947245766f,211.5190773416168213f,-40.2053262983508262f,3.4752408569357769f,3.4752408569354896f,-40.2053262983480195f,211.5190773416065895f,-667.3178725947053636f,1400.4260546581115250f,-2046.8703877316861508f,2116.6332104279072155f,-1539.9760505245460536f,773.6018335460136086f,-256.3575489667678653f,50.5845892778491475f,-4.5128199923709218f,-2.1887762980337655f,25.8312329879517399f,-139.1943156599222959f,452.2089481371749571f,-984.2496748237388147f,1506.3864783952221842f,-1651.5351959878282742f,1291.0440217153050071f,-697.6824145723879838f,247.3030965241330534f,-51.7887376175847578f,4.8653371997095931f,1.0926783309286083f,-13.0592043650811398f,71.4608193709667177f,-236.6382638889151337f,527.7551190476762031f,-834.0169104939160434f,955.2283888889874106f,-794.5948214286507891f,469.8954343033963710f,-184.3563260582184284f,42.6106388888929217f,-4.3775525960669945f,-0.4039754689755742f,4.8661031746037908f,-26.8845238095267405f,90.1050595238188947f,-204.1023809524020578f,329.3338888889226155f,-388.4246666667048657f,337.7327380952694966f,-215.0718253968435931f,97.4525793650863790f,-26.8810317460333899f,3.2780349927852006f,0.0909090909091148f,-1.1000000000001293f,6.1111111111117227f,-20.6250000000019931f,47.1428571428617715f,-77.0000000000076028f,92.4000000000087880f,-82.5000000000073186f,55.0000000000042988f,-27.5000000000016627f,11.0000000000003908f,-2.0198773448773943f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f,1.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,-0.0000000000000000f,-2.1032106782109197f,12.0000000000025899f,-33.0000000000131237f,73.3333333333735737f,-123.7500000000827072f,158.4000000001210537f,-154.0000000001284661f,113.1428571429569274f,-61.8750000000560192f,24.4444444444663702f,-6.6000000000056351f,1.0909090909099497f,-0.0833333333333881f,3.5916948707868439f,-31.0658795749819845f,121.4311688312273105f,-296.5137085138885595f,522.8668831172550426f,-686.5496103901571132f,678.6787878793709297f,-504.4987012991570623f,278.3084415586993714f,-110.6897546898565281f,30.0462337662603005f,-4.9879574970525056f,0.3824019414931286f,-5.1019370326670863f,52.6438158205734226f,-244.5465974027524965f,681.3451082255872961f,-1286.3799350659278389f,1759.7772467547126780f,-1787.6320000015709866f,1354.8417662350011597f,-758.3037662344697765f,305.0001731604537554f,-83.5358961039701029f,13.9695301062690191f,-1.0775084612365404f,6.1050225195732066f,-69.1542038568069017f,354.5040346323302174f,-1086.9641173650334167f,2223.8296103915167805f,-3215.1438961067233322f,3395.8878383868850506f,-2648.5460779244863261f,1515.1272727286532245f,-619.9562097167637376f,172.1627705629177569f,-29.1195718221405286f,2.2675275700746624f,-6.1859348900333169f,74.0168752460488832f,-403.1679653683897300f,1319.7046657059843255f,-2886.4370129897683910f,4439.5158441599442085f,-4928.8711111155444087f,3998.1610389645575196f,-2359.6347402617652733f,990.0965848974014989f,-280.6782683984864661f,48.2956631247886889f,-3.8156390747324309f,5.3022299057446212f,-65.6477112498806008f,371.8203339522331135f,-1272.4360338089270499f,2925.6336734724532107f,-4751.0595918413046093f,5573.7741991391512784f,-4751.0595918406725104f,2925.6336734716596766f,-1272.4360338083813531f,371.8203339520089230f,-65.6477112498287170f,5.3022299057392672f,-3.8156390747370317f,48.2956631248327355f,-280.6782683986740494f,990.0965848978499935f,-2359.6347402623928247f,3998.1610389649981698f,-4928.8711111154043465f,4439.5158441592620875f,-2886.4370129889875898f,1319.7046657054643219f,-403.1679653681792956f,74.0168752460006090f,-6.1859348900283067f,2.2675275700777329f,-29.1195718221700837f,172.1627705630425282f,-619.9562097170568222f,1515.1272727290468083f,-2648.5460779247250684f,3395.8878383867099728f,-3215.1438961061826376f,2223.8296103909301564f,-1086.9641173646505194f,354.5040346321766833f,-69.1542038567716872f,6.1050225195695802f,-1.0775084612381201f,13.9695301062842230f,-83.5358961040342507f,305.0001731606031399f,-758.3037662346662273f,1354.8417662351084800f,-1787.6320000014554807f,1759.7772467544054962f,-1286.3799350656036040f,681.3451082253773166f,-244.5465974026687093f,52.6438158205543232f,-5.1019370326651021f,0.3824019414937217f,-4.9879574970582192f,30.0462337662841854f,-110.6897546899113678f,278.3084415587690614f,-504.4987012991892925f,678.6787878793167010f,-686.5496103900310345f,522.8668831171254396f,-296.5137085138056818f,121.4311688311943982f,-31.0658795749744456f,3.5916948707860579f,-0.0833333333335235f,1.0909090909112331f,-6.6000000000110051f,24.4444444444786591f,-61.8750000000715374f,113.1428571429637344f,-154.0000000001154774f,158.4000000000918078f,-123.7500000000528075f,73.3333333333544601f,-33.0000000000055280f,12.0000000000008527f,-2.1032106782107376f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,0.0000000000000000f,1.0000000000000000f};
	float invData[]={1.2071067811865475f,-0.2071067811865475f,-0.2071067811865475f, 1.2071067811865475f, 1.2440169358562922f,-0.3333333333333332f, 0.0893163974770408f,-0.6666666666666666f, 2.3333333333333335f,-0.6666666666666664f, 0.0893163974770408f,-0.3333333333333329f, 1.2440169358562920f, 1.2568348730314620f,-0.3741514406663724f, 0.1670446594798248f,-0.0497280918449145f,-1.1419843098162998f, 3.2127350543278550f,-1.5342237523502755f, 0.4634730078387203f, 0.4634730078387206f,-1.5342237523502764f, 3.2127350543278554f,-1.1419843098162996f,-0.0497280918449145f, 0.1670446594798249f,-0.3741514406663725f, 1.2568348730314622f, 1.2627503029350082f,-0.3925221011010305f, 0.2000000000000003f,-0.1019050898988858f, 0.0316768880649072f,-1.6215424319502632f, 4.0378905552715416f,-2.2000000000000011f, 1.1398182087281219f,-0.3561663320493996f, 1.1027346441664567f,-3.3694013108331249f, 5.5333333333333377f,-3.3694013108331231f, 1.1027346441664554f,-0.3561663320494003f, 1.1398182087281230f,-2.2000000000000020f, 4.0378905552715407f,-1.6215424319502618f, 0.0316768880649072f,-0.1019050898988857f, 0.1999999999999999f,-0.3925221011010295f, 1.2627503029350080f, 1.2659590187875254f,-0.4023689270621822f, 0.2172042288068674f,-0.1278878313298266f, 0.0690355937288491f,-0.0219420829312326f,-2.1026362611683109f, 4.8422342434919594f,-2.7933160453948038f, 1.6730595402419131f,-0.9089009101586263f, 0.2895594329878686f, 2.0086666844837899f,-5.8059571850063403f, 8.3795304066834362f,-5.8379066964846276f, 3.3392905183396731f,-1.0836237280159320f,-1.0836237280159322f, 3.3392905183396726f,-5.8379066964846276f, 8.3795304066834397f,-5.8059571850063421f, 2.0086666844837904f, 0.2895594329878688f,-0.9089009101586267f, 1.6730595402419142f,-2.7933160453948065f, 4.8422342434919621f,-2.1026362611683114f,-0.0219420829312327f, 0.0690355937288493f,-0.1278878313298270f, 0.2172042288068682f,-0.4023689270621836f, 1.2659590187875256f, 1.2678922107099655f,-0.4082622584674826f, 0.2273558404260387f,-0.1428571428571428f, 0.0897630921953162f,-0.0499878762781389f, 0.0160961342714444f,-2.5843907089195381f, 5.6362492645480815f,-3.3522541295675832f, 2.1428571428571437f,-1.3555002890017540f, 0.7571293659496972f,-0.2440906458660498f, 3.1817094299159048f,-8.8316938658543513f,11.7759194126794980f,-8.6761904761904809f, 5.7761225771853217f,-3.2987762604643844f, 1.0729091827284982f,-2.4066189702966967f, 7.2027515123269978f,-11.7675611134588800f,14.9428571428571480f,-11.7675611134588770f, 7.2027515123270014f,-2.4066189702966962f, 1.0729091827284973f,-3.2987762604643804f, 5.7761225771853208f,-8.6761904761904791f,11.7759194126794940f,-8.8316938658543567f, 3.1817094299159052f,-0.2440906458660499f, 0.7571293659496970f,-1.3555002890017556f, 2.1428571428571459f,-3.3522541295675872f, 5.6362492645480877f,-2.5843907089195386f, 0.0160961342714445f,-0.0499878762781392f, 0.0897630921953167f,-0.1428571428571435f, 0.2273558404260399f,-0.4082622584674848f, 1.2678922107099664f, 1.2691462984511075f,-0.4120697761172902f, 0.2338585514736737f,-0.1523129406984969f, 0.1025848488535825f,-0.0668138919938489f, 0.0379183354509178f,-0.0123114254196455f,-3.0664625293760079f, 6.4243269848694640f,-3.8920694988534432f, 2.5787723024128266f,-1.7487270805143698f, 1.1426540237673830f,-0.6495301484028787f, 0.2110359460970218f, 4.6220055946571810f,-12.4410262283486530f,15.7268005734170520f,-11.9241767448699590f, 8.4980896982042307f,-5.6817177340044189f, 3.2663326552718344f,-1.0663078143272522f,-4.4998221557608176f,13.1613758492908860f,-20.4791530371734180f,24.2265419554823570f,-20.0372656299809610f,14.3130510896251210f,-8.4953759399559701f, 2.8106478684727763f, 2.8106478684727745f,-8.4953759399559718f,14.3130510896251280f,-20.0372656299809540f,24.2265419554824040f,-20.4791530371734360f,13.1613758492909000f,-4.4998221557608264f,-1.0663078143272513f, 3.2663326552718357f,-5.6817177340044234f, 8.4980896982042378f,-11.9241767448699890f,15.7268005734170570f,-12.4410262283486560f, 4.6220055946571845f, 0.2110359460970216f,-0.6495301484028790f, 1.1426540237673841f,-1.7487270805143711f, 2.5787723024128324f,-3.8920694988534423f, 6.4243269848694649f,-3.0664625293760097f,-0.0123114254196455f, 0.0379183354509178f,-0.0668138919938491f, 0.1025848488535827f,-0.1523129406984974f, 0.2338585514736738f,-0.4120697761172904f, 1.2691462984511079f, 1.2700058114179273f,-0.4146723119520984f, 0.2382785467232847f,-0.1586831118602351f, 0.1111111111111112f,-0.0778008375788568f, 0.0518119620172222f,-0.0297721324923471f, 0.0097209626139916f,-3.5486985550223111f, 7.2086529367292380f,-4.4200995091909068f, 2.9943581214826898f,-2.1111111111111103f, 1.4831728913119016f,-0.9894955061099595f, 0.5691248410485412f,-0.1859041091380878f, 6.3296034390529448f,-16.6310217721114740f,20.2325441679739380f,-15.5968506606984400f,11.5396825396825630f,-8.2947527577775304f, 5.6008333618383572f,-3.2419941009044333f, 1.0619557829440889f,-7.5379383419359343f,21.6437458275678410f,-32.4655140757598200f,36.6977308010244270f,-31.0634920634921590f,23.7575580184031980f,-16.5667348746788610f, 9.7530795692576788f,-3.2184348603863984f, 6.0570047950483543f,-18.0507936507937430f,29.5065434286212960f,-39.3159291760505520f,44.6063492063493430f,-39.3159291760505310f,29.5065434286212560f,-18.0507936507937360f, 6.0570047950483570f,-3.2184348603863970f, 9.7530795692576877f,-16.5667348746788970f,23.7575580184032130f,-31.0634920634921770f,36.6977308010244490f,-32.4655140757597990f,21.6437458275678340f,-7.5379383419359351f, 1.0619557829440911f,-3.2419941009044431f, 5.6008333618383777f,-8.2947527577775464f,11.5396825396825820f,-15.5968506606984520f,20.2325441679739270f,-16.6310217721114740f, 6.3296034390529483f,-0.1859041091380886f, 0.5691248410485442f,-0.9894955061099654f, 1.4831728913119073f,-2.1111111111111160f, 2.9943581214826938f,-4.4200995091909014f, 7.2086529367292353f,-3.5486985550223111f, 0.0097209626139916f,-0.0297721324923470f, 0.0518119620172221f,-0.0778008375788566f, 0.1111111111111109f,-0.1586831118602345f, 0.2382785467232831f,-0.4146723119520955f, 1.2700058114179260f, 1.2706204736174693f,-0.4165299770090410f, 0.2414213562373098f,-0.1631851687128795f, 0.1170849566112543f,-0.0854080685463469f, 0.0612800788139934f,-0.0414213562373096f, 0.0240078759080116f,-0.0078701706824618f,-4.0310238571005810f, 7.9904462503522957f,-4.9403278782113844f, 3.3967366393217930f,-2.4539590346025508f, 1.7961368646864628f,-1.2911336427726516f, 0.8736612115447001f,-0.5066791845687396f, 0.1661426313506582f, 8.3045178246089311f,-21.3999871152317240f,25.2927056076446100f,-19.7006775498374120f,14.9166575622646410f,-11.1669528354526800f, 8.1260640960538133f,-5.5371500520889656f, 3.2237589962393294f,-1.0589365342005472f,-11.6956781968065280f,33.0771903948482590f,-48.2201294830092590f,52.8147046182650540f,-45.2624539547666560f,35.9186760407173580f,-26.9685769486997840f,18.7058437687233900f,-10.9982868502734910f, 3.6287106110016722f,11.4915255927126320f,-33.8440940911414220f,53.9988678703192870f,-69.3847245979693100f,75.6905546049499800f,-67.9203897255257370f,54.5139085688566850f,-39.2718837433349610f,23.5795482977633920f,-7.8533127766305624f,-7.8533127766306050f,23.5795482977635090f,-39.2718837433350880f,54.5139085688567630f,-67.9203897255257230f,75.6905546049499090f,-69.3847245979691390f,53.9988678703190740f,-33.8440940911412510f,11.4915255927125680f, 3.6287106110016820f,-10.9982868502735180f,18.7058437687234080f,-26.9685769486997520f,35.9186760407172730f,-45.2624539547665420f,52.8147046182648980f,-48.2201294830090890f,33.0771903948481170f,-11.6956781968064740f,-1.0589365342005488f, 3.2237589962393320f,-5.5371500520889612f, 8.1260640960537938f,-11.1669528354526390f,14.9166575622645880f,-19.7006775498373370f,25.2927056076445210f,-21.3999871152316460f, 8.3045178246089009f, 0.1661426313506579f,-0.5066791845687382f, 0.8736612115446962f,-1.2911336427726436f, 1.7961368646864502f,-2.4539590346025348f, 3.3967366393217735f,-4.9403278782113640f, 7.9904462503522744f,-4.0310238571005721f,-0.0078701706824618f, 0.0240078759080116f,-0.0414213562373094f, 0.0612800788139930f,-0.0854080685463463f, 0.1170849566112535f,-0.1631851687128786f, 0.2414213562373089f,-0.4165299770090402f, 1.2706204736174693f, 1.2710751777368867f,-0.4179023464128036f, 0.2437367052181599f,-0.1664875696402384f, 0.1214403281058893f,-0.0909090909090920f, 0.0680536930261863f,-0.0496401192459963f, 0.0339073378485210f,-0.0197760622328593f, 0.0065019465053453f,-4.5133992011592872f, 8.7704443762909783f,-5.4551122116507118f, 3.7900247356838710f,-2.7836008180651555f, 2.0909090909090975f,-1.5682266621478851f, 1.1452066792567057f,-0.7827878036766012f, 0.4567334528837671f,-0.1501916383247728f,10.5467511712689270f,-26.7468735517210310f,30.9067382370106290f,-24.2387390619466280f,18.6369611503241370f,-14.3131313131315480f,10.8672135931926250f,-7.9933980286881061f, 5.4876267583218779f,-3.2099057527472485f, 1.0567567981163357f,-17.1477504785633990f,47.8883711180029510f,-68.2365987820820980f,73.0325215695589240f,-63.0370912878123090f,51.1575757575765880f,-40.0303892680303550f,29.9712399710813280f,-20.7964565848592680f,12.2392986580914640f,-4.0407206729637499f,19.9190710670505740f,-58.0807411927450300f,90.8520330044910910f,            -113.5454881683560900f,120.6398870824923400f,            -109.1956709956727800f,90.8077324924566080f,-70.4812854059118620f,49.9779928489924860f,-29.7823263910338590f, 9.8887956582363774f,-16.6422023868204610f,49.5720031280962490f,-81.2142216423746110f,109.8669882870535900f,            -132.2232600266484300f,142.2813852813875100f,            -132.2232600266484600f,109.8669882870533600f,-81.2142216423740420f,49.5720031280957440f,-16.6422023868202760f, 9.8887956582365124f,-29.7823263910342210f,49.9779928489929120f,-70.4812854059121040f,90.8077324924566650f,            -109.1956709956728300f,120.6398870824924200f,            -113.5454881683559600f,90.8520330044906070f,-58.0807411927445540f,19.9190710670503960f,-4.0407206729638210f,12.2392986580916500f,-20.7964565848594840f,29.9712399710814630f,-40.0303892680304190f,51.1575757575766450f,-63.0370912878124160f,73.0325215695589090f,-68.2365987820818420f,47.8883711180026670f,-17.1477504785632920f, 1.0567567981163533f,-3.2099057527472934f, 5.4876267583219267f,-7.9933980286881336f,10.8672135931926410f,-14.3131313131315710f,18.6369611503241970f,-24.2387390619466600f,30.9067382370105720f,-26.7468735517209450f,10.5467511712688950f,-0.1501916383247787f, 0.4567334528837838f,-0.7827878036766250f, 1.1452066792567326f,-1.5682266621479142f, 2.0909090909091290f,-2.7836008180651910f, 3.7900247356838990f,-5.4551122116507198f, 8.7704443762909694f,-4.5133992011592809f, 0.0065019465053454f,-0.0197760622328596f, 0.0339073378485214f,-0.0496401192459966f, 0.0680536930261865f,-0.0909090909090921f, 0.1214403281058894f,-0.1664875696402379f, 0.2437367052181577f,-0.4179023464127987f, 1.2710751777368843f, 1.2714209740221285f,-0.4189449576771557f, 0.2454920837121500f,-0.1689832834991028f, 0.1247171468887914f,-0.0950234548472962f, 0.0730813719160634f,-0.0556815531599417f, 0.0410954521692754f,-0.0282878549052813f, 0.0165760306149715f,-0.0054619552346032f,-4.9958029611765706f, 9.5491212557954945f,-5.9659406667544213f, 4.1767756348566820f,-3.1038715893407085f, 2.3729927335923295f,-1.8285684239433730f, 1.3948408123650464f,-1.0302136996924272f, 0.7094716697400607f,-0.4158480545774109f, 0.1370432891353023f,13.0563013095615460f,-32.6709957994938160f,37.0741588240381650f,-29.2126148041457900f,22.7049245779630410f,-17.7414386060120850f,13.8380551514039920f,-10.6331271936527240f, 7.8895333283647977f,-5.4488631979860864f, 3.1991984151835200f,-1.0551320052245794f,-24.0688605316471820f,66.5035670837471710f,-93.0084892059984870f,97.8048278560323330f,-84.7842192313018900f,69.8244128554967740f,-56.0673517046035530f,43.8388674333082590f,-32.8833719717193280f,22.8660568034810370f,-13.4794274069657600f, 4.4539880201706881f,32.2698146337479980f,-93.2923159116544980f,143.5478030370348100f,            -175.5277646546474000f,183.0060368862579600f,            -166.3844635564920500f,141.3160978389637100f,            -114.3112681395440700f,87.5909409903118790f,-61.7277235812830720f,36.6763350437286560f,-12.1634925864241090f,-31.8702027546006970f,94.2788558383910380f,            -152.3211226301371800f,201.8117680515327100f,            -236.6548241280943600f,248.8595441590595700f,            -232.6090961140254500f,199.9220644435865500f,            -159.2407422802557700f,115.0053881756803800f,-69.3310889388764760f,23.1494561777398360f,23.1494561777399180f,-69.3310889388767460f,115.0053881756806600f,            -159.2407422802563400f,199.9220644435872300f,            -232.6090961140257900f,248.8595441590590800f,            -236.6548241280934500f,201.8117680515319400f,            -152.3211226301367000f,94.2788558383905840f,-31.8702027546005550f,-12.1634925864242490f,36.6763350437290820f,-61.7277235812836410f,87.5909409903127030f,            -114.3112681395449900f,141.3160978389644500f,            -166.3844635564922500f,183.0060368862578700f,            -175.5277646546474000f,143.5478030370349500f,-93.2923159116543700f,32.2698146337479340f, 4.4539880201707698f,-13.4794274069660020f,22.8660568034813810f,-32.8833719717197890f,43.8388674333087850f,-56.0673517046040150f,69.8244128554970300f,-84.7842192313020460f,97.8048278560325460f,-93.0084892059986860f,66.5035670837471860f,-24.0688605316472040f,-1.0551320052245907f, 3.1991984151835537f,-5.4488631979861317f, 7.8895333283648643f,-10.6331271936527990f,13.8380551514040530f,-17.7414386060121100f,22.7049245779630590f,-29.2126148041458510f,37.0741588240382210f,-32.6709957994937950f,13.0563013095615350f, 0.1370432891353059f,-0.4158480545774216f, 0.7094716697400764f,-1.0302136996924476f, 1.3948408123650688f,-1.8285684239433919f, 2.3729927335923415f,-3.1038715893407174f, 4.1767756348566971f,-5.9659406667544301f, 9.5491212557954785f,-4.9958029611765618f,-0.0054619552346031f, 0.0165760306149713f,-0.0282878549052810f, 0.0410954521692749f,-0.0556815531599411f, 0.0730813719160625f,-0.0950234548472948f, 0.1247171468887895f,-0.1689832834991007f, 0.2454920837121468f,-0.4189449576771494f, 1.2714209740221252f, 1.2716900588505595f,-0.4197556455719107f, 0.2468548329263462f,-0.1709160656476315f, 0.1272463114709939f,-0.0981850533832088f, 0.0769230769230778f,-0.0602653821475202f, 0.0465016211072067f,-0.0346202666255663f, 0.0239702001908114f,-0.0140966770208696f, 0.0046529889277126f,-5.4782227447360414f,10.3267961573574500f,-6.4738008016711328f, 4.5586607840226856f,-3.4172380336623571f, 2.6458481776560641f,-2.0769230769231619f, 1.6291048897400195f,-1.2580027534166294f, 0.9370478393071140f,-0.6489989581425676f, 0.3817455518583408f,-0.1260170313897932f,15.8331645798998530f,-39.1718865836598130f,43.7945737968536120f,-34.6231591468990130f,27.1230580839729600f,-21.4566483769435340f,17.0466200466204400f,-13.4692158835440650f,10.4497341628370390f,-7.8075048101931985f, 5.4181554616092917f,-3.1907799890706436f, 1.0538886585170966f,-32.6337104484296820f,89.3488251227561390f,            -123.0293034485151700f,127.5845589976498200f,            -110.8975308907871700f,92.2634329075132910f,-75.3860139860152000f,60.5875258462754050f,-47.5183032717833950f,35.7556693087254730f,-24.9269797072191320f,14.7200256601771410f,-4.8681960903475803f,49.5992400796531640f,            -142.3363658148639700f,215.9876759694133700f,            -259.4880831775052500f,266.7331527388970500f,            -243.0755399192913700f,209.2728826728857400f,            -173.6761307061283900f,139.0463448396471100f,            -106.0464901551677000f,74.5768507560371030f,-44.2709707822130060f,14.6774334986362670f,-56.5316679274069430f,166.2285981121165400f,            -265.3848515324946800f,345.6256642179677700f,            -397.2296551035392400f,410.9878281323425400f,            -385.1072261072305900f,337.5581763820765700f,            -280.1154184371628200f,218.7636973489210800f,            -156.2358120610441900f,93.6134712874875560f,-31.1728043120338430f,48.5355379344641950f,            -144.6445532203094300f,237.5342746453155800f,            -323.8711362929477700f,398.2944321556750000f,            -452.3047656784100900f,473.9124209124257700f,            -452.3047656784115100f,398.2944321556777300f,            -323.8711362929503300f,237.5342746453172300f,            -144.6445532203105100f,48.5355379344644150f,-31.1728043120335380f,93.6134712874863200f,            -156.2358120610421700f,218.7636973489180700f,            -280.1154184371594100f,337.5581763820739000f,            -385.1072261072291700f,410.9878281323422600f,            -397.2296551035399800f,345.6256642179683400f,            -265.3848515324947900f,166.2285981121165700f,-56.5316679274067580f,14.6774334986359760f,-44.2709707822119750f,74.5768507560354410f,            -106.0464901551653700f,139.0463448396444100f,            -173.6761307061258900f,209.2728826728838100f,            -243.0755399192901200f,266.7331527388965400f,            -259.4880831775046200f,215.9876759694125500f,            -142.3363658148634600f,49.5992400796528370f,-4.8681960903474568f,14.7200256601767290f,-24.9269797072184640f,35.7556693087245280f,-47.5183032717822870f,60.5875258462743530f,-75.3860139860143190f,92.2634329075126230f,            -110.8975308907867800f,127.5845589976493200f,            -123.0293034485146100f,89.3488251227557980f,-32.6337104484294680f, 1.0538886585170522f,-3.1907799890705051f, 5.4181554616090759f,-7.8075048101929090f,10.4497341628367050f,-13.4692158835437330f,17.0466200466201380f,-21.4566483769432780f,27.1230580839727830f,-34.6231591468988000f,43.7945737968533990f,-39.1718865836596990f,15.8331645798997820f,-0.1260170313897853f, 0.3817455518583163f,-0.6489989581425281f, 0.9370478393070596f,-1.2580027534165641f, 1.6291048897399503f,-2.0769230769230940f, 2.6458481776560010f,-3.4172380336623038f, 4.5586607840226305f,-6.4738008016710902f,10.3267961573574350f,-5.4782227447360290f, 0.0046529889277125f,-0.0140966770208694f, 0.0239702001908112f,-0.0346202666255661f, 0.0465016211072067f,-0.0602653821475205f, 0.0769230769230788f,-0.0981850533832104f, 0.1272463114709962f,-0.1709160656476343f, 0.2468548329263502f,-0.4197556455719178f, 1.2716900588505631f};
	if(degSum>12||degSum<1)
	{
		printf("The surface degree %d should not greater than 12 or less than 1!\n",degSum);
		exit(0);
	}

	int ind=degSum*(degSum+1)*(2*degSum+1)/6-1;
	CUDA_SAFE_CALL( cudaMalloc((void**)&dInvMatrix, (degSum+1)*(degSum+1)*sizeof(float)) ) ;
	CUDA_SAFE_CALL( cudaMemcpy(dInvMatrix, invData+ind, (degSum+1)*(degSum+1)*sizeof(float), cudaMemcpyHostToDevice) );
}


extern "C"
void InitMulitSilSurfaceCUDA(float* pFuncData, int* Index, int* indexInverse, int NumPatch, MULTI_SIL_SURFACE* multiSilSurface)
{
	patchBits = 8*sizeof(uint)-2*pixelBits;
	int maxNumPatch = 1<<patchBits;
	if (NumPatch>maxNumPatch)
	{
		printf("Too many patches!\n"); 
		return;
	}

	pixelMask = (1<<pixelBits)-1;
	patchMask = (1<<patchBits)-1;

	nPatch = NumPatch;
	asi.deg[0]=multiSilSurface->degX; asi.deg[1]=multiSilSurface->degY; asi.deg[2]=multiSilSurface->degZ;
	asi.seg[0]=multiSilSurface->segX; asi.seg[1]=multiSilSurface->segY; asi.seg[2]=multiSilSurface->segZ;
	asi.segShift[0] = asi.seg[1]*asi.seg[2]; asi.segShift[1] = asi.seg[2]; asi.segShift[2] = 1;
	maxSeg = max(asi.seg[0], asi.seg[1]); maxSeg = max(maxSeg, asi.seg[2]);
	degSize = (asi.deg[0]+1)*(asi.deg[1]+1)*(asi.deg[2]+1); 

	InitInvMatrix(asi.deg[0]+asi.deg[1]+asi.deg[2]);

	int segSize = asi.seg[0]*asi.segShift[0];


	int funcSize = nPatch*degSize*sizeof(float);	
	CUDA_SAFE_CALL( cudaMalloc((void**)&dFuncData, funcSize) );
	CUDA_SAFE_CALL( cudaMemcpy(dFuncData, pFuncData, funcSize, cudaMemcpyHostToDevice) );
	//cudaMemcpyToSymbol(dFuncData, pFuncData, size);	

	CUDA_SAFE_CALL( cudaMalloc((void**)&dPatchIndex, segSize*sizeof(int)) );
	CUDA_SAFE_CALL( cudaMemcpy(dPatchIndex, Index, segSize*sizeof(int), cudaMemcpyHostToDevice) );
	//cudaMemcpyToSymbol(dPatchIndex, Index, segX*segY*segZ*sizeof(uint));

	CUDA_SAFE_CALL( cudaMalloc((void**)&dPatchIndexInverse, nPatch*sizeof(int)) ) ;
	CUDA_SAFE_CALL( cudaMemcpy(dPatchIndexInverse, indexInverse, nPatch*sizeof(int), cudaMemcpyHostToDevice) );

	CUDA_SAFE_CALL( cudaMalloc((void**)&dPolarData, funcSize) );
	//cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
	CUDA_SAFE_CALL( cudaBindTexture(0, polarTex, dPolarData, cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat)/*channelDesc*/) );

	CUDA_SAFE_CALL( cudaMalloc((void**)&dPolarIndex, nPatch*sizeof(int)) );
	
	//CUDA_SAFE_CALL( cudaMalloc( (void**) &dMaxSilPoint, sizeof(size_t)));
    CUDA_SAFE_CALL( cudaMalloc( (void**) &dNumber, sizeof(size_t)));
	

#if	UNIFORM_KNOT	
	asi.box0[0] = multiSilSurface->boxX[0];
	asi.box0[1] = multiSilSurface->boxY[0];
	asi.box0[2] = multiSilSurface->boxZ[0];
	asi.boxSize[0] = multiSilSurface->boxX[1]-multiSilSurface->boxX[0];
	asi.boxSize[1] = multiSilSurface->boxY[1]-multiSilSurface->boxY[0];
	asi.boxSize[2] = multiSilSurface->boxZ[1]-multiSilSurface->boxZ[0];
#else	
	CUDA_SAFE_CALL( cudaMalloc((void**)&dBoxX, (asi.seg[0]+1)*sizeof(float)) );
	CUDA_SAFE_CALL( cudaMalloc((void**)&dBoxY, (asi.seg[1]+1)*sizeof(float)) );
	CUDA_SAFE_CALL( cudaMalloc((void**)&dBoxZ, (asi.seg[2]+1)*sizeof(float)) );
	CUDA_SAFE_CALL( cudaMemcpy(dBoxX, multiSilSurface->boxX, (asi.seg[0]+1)*sizeof(float), cudaMemcpyHostToDevice) );	
	CUDA_SAFE_CALL( cudaMemcpy(dBoxY, multiSilSurface->boxY, (asi.seg[1]+1)*sizeof(float), cudaMemcpyHostToDevice) );
	CUDA_SAFE_CALL( cudaMemcpy(dBoxZ, multiSilSurface->boxZ, (asi.seg[2]+1)*sizeof(float), cudaMemcpyHostToDevice) );
	//cudaMemcpyToSymbol(dBoxX, multiSilSurface->boxX, (asi.seg[0]+1)*sizeof(float));
	//cudaMemcpyToSymbol(dBoxY, multiSilSurface->boxY, (asi.seg[1]+1)*sizeof(float));
	//cudaMemcpyToSymbol(dBoxZ, multiSilSurface->boxZ, (asi.seg[2]+1)*sizeof(float));
#endif
}

extern "C"
void CleanUpSurfaceData()
{
	CUDA_SAFE_CALL( cudaFree(dFuncData) );
	CUDA_SAFE_CALL( cudaFree(dPatchIndex) );
	CUDA_SAFE_CALL( cudaFree(dPatchIndexInverse) );

	CUDA_SAFE_CALL( cudaUnbindTexture(polarTex) );
	CUDA_SAFE_CALL( cudaFree(dPolarData) );

	CUDA_SAFE_CALL( cudaFree(dPolarIndex) );
	//CUDA_SAFE_CALL( cudaFree(cRenderParam) );

	//CUDA_SAFE_CALL( cudaFree(dMaxSilPoint) );
	CUDA_SAFE_CALL( cudaFree(dNumber) );

#ifndef UNIFORM_KNOT	
	CUDA_SAFE_CALL( cudaFree(dBoxX) );
	CUDA_SAFE_CALL( cudaFree(dBoxY) );
	CUDA_SAFE_CALL( cudaFree(dBoxZ) );
#endif

}

extern "C"
void ResetRenderData(int surfWidth, int surfHeight)
{
	renderWidth = surfWidth;
	renderHeight = surfHeight;
 	allPointSize = renderWidth*renderHeight*sizeof(uint);
	CUDA_SAFE_CALL( cudaMalloc((void**)&dSilOccupied, allPointSize) );		
	CUDA_SAFE_CALL( cudaMalloc((void**)&dSilOccupiedValid, allPointSize) );

	CUDA_SAFE_CALL( cudaMalloc((void**)&dPointOccupied, allPointSize) );		
	CUDA_SAFE_CALL( cudaMalloc((void**)&dPointOccupiedValid, allPointSize) );

	CUDA_SAFE_CALL( cudaMalloc((void**)&dOccupiedCompact, allPointSize) );
	CUDA_SAFE_CALL( cudaMalloc((void**)&dOccupiedSort, allPointSize) );

	CUDA_SAFE_CALL( cudaMalloc((void**)&dSil, renderWidth*45*sizeof(float)) );
	//CUDA_SAFE_CALL( cudaMalloc((void**)&dError, renderWidth*10*sizeof(uint)) );
	//CUDA_SAFE_CALL( cudaMalloc((void**)&dBoundary, renderWidth*35*sizeof(uint)) );

	//uint numSilPoint = allPointSize/30;
	//silPointOccupied = (uint*)malloc(numSilPoint);
	//silPointOccupiedPad = (uint*)malloc(numSilPoint);
#if SORT_SHARE
	uint numSortPad = allPointSize+sizeof(uint);
	pointOccupiedSort = (uint*)malloc(numSortPad);
	pointOccupiedPad = (uint*)malloc(numSortPad*5/3);
	CUDA_SAFE_CALL( cudaMalloc((void**)&dOccupiedSortPad, numSortPad*5/3) );
#endif

#if MY_DEBUG_CUDA
	CUDA_SAFE_CALL( cudaMalloc((void**)&dDebugInfo, allPointSize) );
#endif

#if TEST
	CUDA_SAFE_CALL( cudaMalloc((void**)&dSilContour, 4*(renderWidth+renderHeight)*sizeof(int)) );
	CUDA_SAFE_CALL( cudaMemset(dSilContour, 0, 4*(renderWidth+renderHeight)*sizeof(int)) );
	hSilContour = (int*)(malloc(4*(renderWidth+renderHeight)*sizeof(int)));	
	CUDA_SAFE_CALL( cudaMalloc((void**)&dSilContourInfo, 4*(renderWidth+renderHeight)*sizeof(float)) );
	CUDA_SAFE_CALL( cudaMemset(dSilContourInfo, 0, 4*(renderWidth+renderHeight)*sizeof(float)) );
	hSilContourInfo = (int*)(malloc(4*(renderWidth+renderHeight)*sizeof(float)));	

	CUDA_SAFE_CALL( cudaMalloc((void**)&dConverEdge, 4*(renderWidth+renderHeight)*sizeof(int)) );
	CUDA_SAFE_CALL( cudaMemset(dConverEdge, 0, 4*(renderWidth+renderHeight)*sizeof(int)) );
	//CUDA_SAFE_CALL( cudaMalloc((void**)&dConverEdgeInfo, 4*(renderWidth+renderHeight)*sizeof(float)) );
	//CUDA_SAFE_CALL( cudaMemset(dConverEdgeInfo, 0, 4*(renderWidth+renderHeight)*sizeof(float)) );
	CUDA_SAFE_CALL( cudaMalloc((void**)&dSilConverEdge, 2*(renderWidth+renderHeight)*sizeof(int)) );
	CUDA_SAFE_CALL( cudaMemset(dSilConverEdge, 0, 2*(renderWidth+renderHeight)*sizeof(int)) );

#endif
}

extern "C"
void CleanUpRenderData()
{
	CUDA_SAFE_CALL( cudaFree(dSilOccupiedValid) );
	CUDA_SAFE_CALL( cudaFree(dSilOccupied) );

	CUDA_SAFE_CALL( cudaFree(dPointOccupied) );
	CUDA_SAFE_CALL( cudaFree(dPointOccupiedValid) );

	CUDA_SAFE_CALL( cudaFree(dOccupiedCompact) );
	CUDA_SAFE_CALL( cudaFree(dOccupiedSort) );

	CUDA_SAFE_CALL( cudaFree(dSil) );
	//CUDA_SAFE_CALL( cudaFree(dBoundary) );
	//CUDA_SAFE_CALL( cudaFree(dError) );

#if MY_DEBUG_CUDA
	CUDA_SAFE_CALL( cudaFree(dDebugInfo) );
#endif

#if TEST
	CUDA_SAFE_CALL( cudaFree(dSilContour) );
	if(hSilContour) free(hSilContour);
	CUDA_SAFE_CALL( cudaFree(dSilContourInfo) );
	if(hSilContourInfo) free(hSilContourInfo);
	CUDA_SAFE_CALL( cudaFree(dConverEdge) );
	
	CUDA_SAFE_CALL( cudaFree(dSilConverEdge) );

#endif

	//if(silPointOccupied)	free(silPointOccupied);
	//if(silPointOccupiedPad)	free(silPointOccupiedPad);
#if SORT_SHARE
	if(pointOccupiedSort)	free(pointOccupiedSort);
	pointOccupiedSort = 0;
	if(pointOccupiedPad)	free(pointOccupiedPad);
	pointOccupiedPad = 0;
	CUDA_SAFE_CALL( cudaFree(dOccupiedSortPad) );
#endif
}


//extern "C"
//void CleanUpMultiRenderCUDA()
//{
//	CleanUpSurfaceData();
//	CleanUpRenderData();
//}